package com.duke.shaking.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.IntentService;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;

import com.duke.shaking.BaseActivity;
import com.duke.shaking.Constants;
import com.duke.shaking.R;
import com.duke.shaking.pojo.User;
import com.duke.shaking.profile.CareerEditActivity;
import com.duke.shaking.profile.HobbiesEditActivity;
import com.duke.shaking.utils.AppUtils;
import com.duke.shaking.utils.PrefUtils;
import com.duke.shaking.widget.StartTextEditActivityHandler;

public class PopupUpdateAlertService extends IntentService {
	private static final String TAG = "PopupUpdateAlertService";
	
	private static int POPUP_ALERT = 0;
	
	private static AlertDialog.Builder builder;
	private static Context context;
	private int intentCode;

	public PopupUpdateAlertService() {
		super("PopupUpdateAlertService");
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
		builder = new Builder(this);
		context = this;
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		User user = (User)intent.getSerializableExtra("user");
		
		if(user != null && user.hasLogin()) {
			if(alreadyAlertOnSameDay()) {
				
			} else {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
				}
				
				Message msg = new Message();
				msg.what = POPUP_ALERT;
				msg.obj = user;
				handler.sendMessage(msg);
			}
		}
	}
	
	private boolean alreadyAlertOnSameDay() {
		Date date = new Date();
		String strPrevDate = PrefUtils.getInstance(this, "ProfileAlert").getString("popDate", null);
		Log.i(TAG, "strPrevDate = " + strPrevDate);
		if(strPrevDate == null) {
			return false;
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date prevDate = format.parse(strPrevDate);
			if(AppUtils.isSameDay(date, prevDate)) {
				return true;
			} else {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * Save the alert popup date to shared preference
	 */
	private void savePopDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = format.format(new Date());
		Log.i(TAG, "save date:" + strDate + " to pref");
		PrefUtils.getInstance(context, "ProfileAlert").addString("popDate", strDate);
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == POPUP_ALERT) {
				User user = (User)msg.obj;
				popupAlert(user);
			}
		}
		
	};
	
	public static boolean isProfileCompleted(User user) {
		if (user == null) {
			return false;
		}
		if(user.getHobbies() == null || user.getHobbies().size() == 0) {
			return false;
		}
		if(user.getSchool() == null || user.getSchool().trim().length() == 0) {
			return false;
		}
		if(user.getJob() == null || user.getJob() == 0) {
			return false;
		}
		if(user.getSignature() == null || user.getSignature().trim().length() == 0) {
			return false;
		}
		return true;
	}
	
	private StartTextEditActivityHandler handler2 = new StartTextEditActivityHandler(this);
	
	private void popupAlert(User user) {
		String msg = "";
		intentCode = -1;
		if(user.getHobbies() == null || user.getHobbies().size() == 0) {
			String sub = "";
			if(user.getSex() == User.sex_female) {
				sub = context.getString(R.string.alert_hobby_gg);
			} else if(user.getSex() == User.sex_male) {
				sub = context.getString(R.string.alert_hobby_mm);
			}
			msg = String.format(context.getString(R.string.alert_hobby_msg), sub);
			
			intentCode = Constants.INTENT_CODE_EDIT_HOBBIES;
		}
		else if(user.getSchool() == null || user.getSchool().trim().length() == 0) {
			String sub = "";
			if(user.getSex() == User.sex_female) {
				sub = context.getString(R.string.alert_school_gg);
			} else if(user.getSex() == User.sex_male) {
				sub = context.getString(R.string.alert_school_mm);
			}
			msg = String.format(context.getString(R.string.alert_school_msg), sub);
			
			intentCode = Constants.INTENT_CODE_EDIT_SCHOOL;
		}
		else if(user.getJob() == 0) {
			String sub = "";
			if(user.getSex() == User.sex_female) {
				sub = context.getString(R.string.alert_job_gg);
			} else if(user.getSex() == User.sex_male) {
				sub = context.getString(R.string.alert_job_mm);
			}
			msg = String.format(context.getString(R.string.alert_job_msg), sub);
			
			intentCode = Constants.INTENT_CODE_EDIT_CAREER;
		}
		else if(user.getSignature() == null || user.getSignature().trim().length() == 0) {
			msg = context.getString(R.string.alert_signature_msg);
			intentCode = Constants.INTENT_CODE_EDIT_SIGNATURE;
		}
		
		if("".equals(msg)) {
			return;
		}
		
		if (BaseActivity.getMe() == null
			|| !BaseActivity.getMe().hasLogin()) {
			return;
		}
		savePopDate();
		
		builder.setMessage(msg).setPositiveButton(R.string.alert_ok_text, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
				startEditActivity();
				
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(R.string.alert_cancel_text, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		AlertDialog alert = builder.create();
		alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.show();
	}
	
	private void startEditActivity() {
		if(intentCode != -1) {
			if(intentCode == Constants.INTENT_CODE_EDIT_HOBBIES) {
				Intent i = new Intent(this, HobbiesEditActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(i);
			} 
			else if(intentCode == Constants.INTENT_CODE_EDIT_CAREER) {
				Intent i = new Intent(this, CareerEditActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(i);
			}
			else {
				Message msg = new Message();
				msg.what = intentCode;
				handler2.sendMessage(msg);
			}
		}
	}

}
